<?php
namespace admin\action;

use herosphp\http\HttpRequest;
use herosphp\utils\AjaxResult;
use herosphp\bean\Beans;

/**--tmpl-use--**/

class LoginLogAction extends CommonAction {
	protected $order = 'addtime DESC';

	public function C_start()
	{
		parent::C_start();	
		/**--tmpl-starttodo--**/
	}

	/**--tmpl-vars--**/

	protected $serviceBean="admin.adminManagerLoginLog.service";


	/**--tmpl-construct--**/

	/**--tmpl-method--**/

	/**-- actionIndex --**/
	public function index(HttpRequest $request) {

		$this->page = $request->getParameter('page', 'intval');
		$this->pagesize = $request->getParameter('pagesize', 'intval');

		if ( $this->page <=0 ) {
			$this->page = 1;
		}
		if ( $this->pagesize <=0 ) {
			$this->pagesize = 15;
		}
		$conditions = array();

		$s_s_addtime = $request->getParameter('s_s_addtime');
		$s_e_addtime = $request->getParameter('s_e_addtime');

		$search_arr = array(
			"select_mix_option1" => $request->getParameter('select_mix_option1'),
			"select_mix_option_value1" => urldecode($request->getParameter('select_mix_option_value1')),

		);
		if (in_array($search_arr['select_mix_option1'], array('ip', 'username'))) {
			switch ($search_arr['select_mix_option1']) {

				case 'ip':
					$conditions['ip'] = '%'.$search_arr['select_mix_option_value1'].'%';
					break;
				case 'username':
					$conditions['username'] = '%'.$search_arr['select_mix_option_value1'].'%';
					break;
			}
		}


		if($s_s_addtime !== '' && $s_s_addtime!== NULL){
			$conditions['addtime'] = array();
			$conditions['addtime']['>'] = $s_s_addtime.' 00:00:00';
		}
		if($s_e_addtime !== '' && $s_s_addtime!== NULL){
			if(!$conditions['addtime']){ $conditions['addtime'] = array();}
			$conditions['addtime']['<='] = $s_e_addtime.' 23:59:59';
		}
        if( $request->getParameter('export')==1 ){
            $this -> setPage(1);
            $this -> setPagesize(2147483648);
            $ids = $request -> getParameter('ids');
            if( is_array($ids) ){
                $conditions['id']['$in'] = $ids;
            }
        }
		$this->setConditions($conditions);
	
		$pagesize = $request->getParameter('pagesize') ? $request->getParameter('pagesize') : $this->getPagesize();
		$order = $request->getParameter('order') ? $request->getParameter('order') : $this->getOrder();
		$order = str_replace('+' , " ",$order);


		$service = Beans::get($this->getServiceBean());
		$total = $service->count($this->getConditions());
		$items = $service->getItems($this->getConditions(), $this->getFields(), $this->getOrder(),
			array($this->getPage(), $this->getPagesize()), $this->getGroup(), $this->getHaving());

        if($request->getParameter('export') == 1){
            $excel = array();
            foreach($items as $k => $v)
            {
                $tmp = array();
                $tmp[] = $v['username'];
                $tmp[] = $v['name'];
                $tmp[] = $v['ip'];
                $tmp[] = $v['addtime'];
                $excel[] = $tmp;
            }
            $header = array("用户名","姓名","登录ip","登录时间");
            phpexcel("管理员",$header,$excel);
            exit();
        }
		$this->generatePageMenu($total);

		$this->setPagesize($pagesize);

		$this->setOrder($order);
		$this->assign('order',$order);
		$this->assign('pagesize',$pagesize);
		$this->assign('search_arr',$search_arr);
		$this->assign('s_s_addtime',$s_s_addtime);
		$this->assign('s_e_addtime',$s_e_addtime);
		$this->assign('page',$this->page);
		$this->assign('items',$items);

		$this->assign("title", "管理员登录日志");
		$this->assign('bread', array('网站设置',$this->getTemplateVar('title')));
		$this->assign('breadUrl', array('javascript:;','javascript:;'));

		$this->setView(strtolower($request->getAction()) . '/index');
	}



	public function indexByMember(HttpRequest $request) {
		$this->page = $request->getParameter('page', 'intval');
		$this->pagesize = $request->getParameter('pagesize', 'intval');

		if ( $this->page <=0 ) {
			$this->page = 1;
		}
		if ( $this->pagesize <=0 ) {
			$this->pagesize = 15;
		}
		$conditions = array();

		$s_s_addtime = $request->getParameter('s_s_addtime');
		$s_e_addtime = $request->getParameter('s_e_addtime');

		$search_arr = array(
			"select_mix_option1" => $request->getParameter('select_mix_option1'),
			"select_mix_option_value1" => urldecode($request->getParameter('select_mix_option_value1')),

		);

		if (in_array($search_arr['select_mix_option1'], array('ip', 'username'))) {
			switch ($search_arr['select_mix_option1']) {

				case 'ip':
					$conditions['ip'] = '%'.$search_arr['select_mix_option_value1'].'%';
					break;
				case 'username':
					$conditions['username'] = '%'.$search_arr['select_mix_option_value1'].'%';
					break;
			}
		}


		if($s_s_addtime !== '' && $s_s_addtime!== NULL){
			$conditions['addtime'] = array();
			$conditions['addtime']['>'] = $s_s_addtime.' 00:00:00';
		}
		if($s_e_addtime !== '' && $s_s_addtime!== NULL){
			if(!$conditions['addtime']){ $conditions['addtime'] = array();}
			$conditions['addtime']['<='] = $s_e_addtime.' 23:59:59';
		}

        if( $request -> getParameter('export') == 1 ){
            $this -> setPage(1);
            $this -> setPagesize(2147483648);
            $ids = $request -> getParameter('ids');
            if( is_array($ids) ){
                $conditions['id']['$in'] = $ids;
            }
        }

		$this->setConditions($conditions);

		$pagesize = $request->getParameter('pagesize') ? $request->getParameter('pagesize') : $this->getPagesize();
		$order = $request->getParameter('order') ? $request->getParameter('order') : $this->getOrder();
		$order = str_replace('+' , " ",$order);


		$logService = Beans::get('member.log.service');
		$total = $logService->count($this->getConditions());
		$items = $logService->getItems($this->getConditions(), $this->getFields(), $this->getOrder(),
			array($this->getPage(), $this->getPagesize()), $this->getGroup(), $this->getHaving());

        if($request->getParameter('export') == 1){
            $excel = array();
            foreach($items as $k => $v)
            {
                $tmp = array();
                $tmp[] = $v['username'];
                $tmp[] = $v['name'];
                $tmp[] = $v['ip'];
                $tmp[] = $v['addtime'];
                $excel[] = $tmp;
            }
            $header = array("用户名","姓名","登录ip","登录时间");
            phpexcel("会员登陆日志列表",$header,$excel);
            exit();
        }

		$this->generatePageMenu($total);

		$this->setPagesize($pagesize);

		$this->setOrder($order);
		$this->assign('order',$order);
		$this->assign('pagesize',$pagesize);
		$this->assign('search_arr',$search_arr);
		$this->assign('s_s_addtime',$s_s_addtime);
		$this->assign('s_e_addtime',$s_e_addtime);
		$this->assign('page',$this->page);
		$this->assign('items',$items);

		$this->assign("title", "会员登录日志");
		$this->assign('bread', array('网站设置',$this->getTemplateVar('title')));
		$this->assign('breadUrl', array('javascript:;','javascript:;'));
		$this->setView(strtolower($request->getAction()) . '/indexByMember');

	}

}
